package demo3;

import com.sun.xml.internal.ws.addressing.WsaTubeHelper;

import java.util.Arrays;
import java.util.Comparator;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 沈明月
 * Date: 2023-01-03
 * Time: 17:05
 */


/*public class Test {
    public static void main(String[] args){
        int[] array = {1,3,2,10,5,3,7};
        Arrays.sort(array);//排序
        System.out.println(Arrays.toString(array));
    }
}*/




/*

class Student implements Comparable<Student>{
    public String name;
    public int age;
    public int score;

    public Student(String name, int age, int score){
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public String toString() {
        return "Student{"+
                "name='" + name + '\''+
                ", age=" +age +
                ",score=" + score +
                '}';
    }
    public int compareTo(Student o){
*/
/*        //按年龄比较

        if(this.age > o.age){
            return 1;
        }else if(this.age < o.age){
            return -1;
        }else{
            return 0;
        }
        //也可以直接写：return this.age - o.age;
        //通过返回值的正负来确定两个学生年龄的大小。*//*



*/
/*
        //按分数比较
        if(this.score > o.score){
            return 1;
        }else if(this.score < o.score){
            return -1;
        }else{
            return 0;
        }
*//*

        //根据姓名比较(一个字母一个字母比较的)
        //equals-->只能比较相不相等，不能比较大小
        if(this.name.compareTo(o.name) > 0){
            return 1;
        }else if(this.name.compareTo(o.name) < 0){
            return -1;
        }else{
            return 0;
        }
    }
}

public class Test {
    public static void main(String[] args) {
        Student[] students = new Student[3];//初始化
        students[0] = new Student("zhangsan", 12, 10);
        students[1] = new Student("lisi", 13, 11);
        students[2] = new Student("wangwu", 10, 9);
*/
/*
        System.out.println(students[0].compareTo(students[1]));//-1(按年龄比较)

        System.out.println(students[0].compareTo(students[1]));//-1(按分数比较)

        System.out.println(students[0].compareTo(students[1]));//1(按姓名比较)
        *//*

        Arrays.sort(students);
        System.out.println(Arrays.toString(students));


    }
}
*/


class Student{
    public String name;
    public int age;
    public int score;

    public Student(String name, int age, int score){
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public String toString() {
        return "Student{"+
                "name='" + name + '\''+
                ", age=" +age +
                ",score=" + score +
                '}';
    }
}

//按年龄比较：
class AgeComparator implements Comparator<Student>{
    public int compare(Student o1, Student o2){
        return o1.age - o2.age;
    }
}

//其他的两种比较方法也是一样的
//按分数比较：
class ScoreComparator implements Comparator<Student>{
    public int compare(Student o1, Student o2){
        return o1.score - o2.score;
    }
}

//按名字比较：（引用类型）
class NameComparator implements Comparator<Student>{
    public int compare(Student o1, Student o2){
        return o1.name.compareTo(o2.name);
    }
}

public class Test {
    public static void main(String[] args) {
        Student[] students = new Student[3];//初始化
        students[0] = new Student("zhangsan", 12, 10);
        students[1] = new Student("lisi", 13, 11);
        students[2] = new Student("wangwu", 10, 9);

        //比较器
        AgeComparator ageComparator = new AgeComparator();

        Arrays.sort(students,ageComparator);
        System.out.println(Arrays.toString(students));
    }
}

